using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Data;
using Framework.Library.Utilities;
using Framework.Library;

namespace Framework.Web.Controls.Menu
{
    public class AdminMenu : AdminMenuConfig
    {
        public AdminMenu()
            : base()
        {
        }

        

        protected override void Render(HtmlTextWriter writer)
        {
            GetCurrentItem();  
            writer.WriteLine("<ul class=\"" + menu + "\" id=\"menu\">");
           
            writer.Write(createMenu());
            writer.WriteLine("</ul>");
        }

        private string createMenu()
        {
            DataRow[] drRoots = DataSource.Select("CommandParentID = 0 and CommandVisible = 1 and CommandEnable = 1");

            StringBuilder sb = new StringBuilder();

            foreach (DataRow row in drRoots)
            {
                sb.Append("<li>");
                if ((ConvertUtility.ToString(row["CommandKey"]).Trim() == string.Empty) && (row["CommandUrl"].ToString() == string.Empty))
                {
                    sb.Append(createHeader(menuLink, row["CommandName"].ToString()));
                }
                else
                {
                    if (row["CommandUrl"].ToString() != string.Empty)
                    {
                        sb.Append(createLink(row["CommandName"].ToString(), row["CommandUrl"].ToString()));
                    }
                    else if (ConvertUtility.ToString(row["CommandKey"]).Trim() != string.Empty)
                    {
                        sb.Append(createLink(row["CommandName"].ToString(), AppEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString()));
                    }
                }
                sb.Append(LoadCommandItems(DataSource, 0, (int)row["CommandID"]));
                sb.Append("</li>");
            }
            return sb.ToString();
        }

        private string LoadCommandItems(DataTable dtCommands, int level, int id)
        {
            DataRow[] drChilds = dtCommands.Select("CommandParentID = " + id + " and CommandVisible = 1 and CommandEnable = 1");
            StringBuilder sb = new StringBuilder();

            if (drChilds.Length > 0)
            {
                sb.Append("<ul>");
                foreach (DataRow row in drChilds)
                {
                    sb.Append("<li>");
                    
                    if ((ConvertUtility.ToString(row["CommandKey"]).Trim() == string.Empty) && (row["CommandUrl"].ToString() == string.Empty))
                    {
                        sb.Append(createHeader(menuSub, row["CommandName"].ToString()));
                    }
                    else
                    {
                        if (row["CommandUrl"].ToString() != string.Empty)
                        {
                            sb.Append(createLink(row["CommandName"].ToString(), row["CommandUrl"].ToString()));
                        }
                        else if (ConvertUtility.ToString(row["CommandKey"]).Trim() != string.Empty)
                        {
                            sb.Append(createLink(row["CommandName"].ToString(), AppEnv.AdminUrlParams(row["CommandKey"].ToString()) + row["CommandParams"].ToString()));
                        }
                    }                    
                    sb.Append(LoadCommandItems(DataSource, level + 1, (int)row["CommandID"]));
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }
            return sb.ToString();
        }

        private string createHeader(string className, string headerText)
        {
            return string.Format("<a href='#' class='{0}'>{1}</a>", className, headerText);
        }

        private string createLink(string linkText, string linkURL)
        {
            return string.Format("<a href='{0}'>{1}</a>", linkURL, linkText);		
        }                

        private void GetCurrentItem()
        {
            if (String.IsNullOrEmpty(_menu))
            {
                menu = "menu";
            }
            else if (String.IsNullOrEmpty(_menuLink))
            {
                menuLink = "menulink";
            }
            else if (String.IsNullOrEmpty(_menuSub))
            {
                menuSub = "sub";
            }
        }
    }
}